<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">    
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm" :rules="rules">
                    <el-form-item label="机构名称" :label-width="formLabelWidth" prop="name">
                        <el-input type="textarea" v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="名字首字母大写" :label-width="formLabelWidth" prop="nameChar">
                        <el-input v-model="addForm.nameChar" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="选择联盟" :label-width="formLabelWidth">
                        <el-select v-model="addForm.allianceId" placeholder="请选择机构分类">
                            <el-option v-for="alliance in allianceList" :key="alliance.id" :label="alliance.name" :value="alliance.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="是否是联盟成员" :label-width="formLabelWidth">
                        <el-select v-model="addForm.isAlliancer" placeholder="请选择">
                            <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="区域" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.area" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="简介" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.about" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.telephone" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="联系地址" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.address" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="网址" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.url" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="传真" :label-width="formLabelWidth">
                        <el-input type="textarea" v-model="addForm.fax" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入机构名" @click="search" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="addEx">新增机构</el-button>
        </div>
        <div>
            <el-table :data="tableData" border fit style="width: 100%">
                <el-table-column prop="name" label="机构名称" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="nameChar" label="名字首字母大写" fixed></el-table-column>
                <el-table-column prop="allianceId" label="联盟" show-overflow-tooltip :formatter="allianceFormatter">
                </el-table-column>
                <el-table-column prop="isAlliancer" label="是否是联盟成员" show-overflow-tooltip :formatter="isAllianceFormatter">
                </el-table-column>
                <el-table-column prop="area" label="区域" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="about" label="简介" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="telephone" label="联系我们" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="address" label="联系地址" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="url" label="网址" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="fax" label="传真" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="state" label="状态" show-overflow-tooltip empty-text :formatter="stateFormatter">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editOrg(scope.row)" size="small">编辑</el-button>
                        <el-button type="text" @click="delOrg(scope.row)" size="small">删除</el-button>
                        <el-switch
                            v-model="scope.row.state === 1 ? true : false" 
                            active-color="#13ce66"
                            @change="switchChange(scope.row)"
                        >
                        </el-switch>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number" 
            layout="total, prev, pager, next, jumper" :total="page.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { org, alliance } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { filterObj, getStore } from './../../utils/local.js'
const { addApi, orgListApi, disableApi, deleteApi, updateApi } = org
const { listApi } = alliance
export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            select_word: '',
            addForm: {},
            formLabelWidth: '120px',
            title: '',
            organizationType: '',
            rules: {
                name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                nameChar: [{ required: true, message: '请输入名字首字母大写', trigger: 'blur'}, {
                    pattern: /^[A-Z]*$/, message: '只支持A-Z大写'
                }],
            },
            options: [{
                value: 0,
                label: '不是'
            }, {
                value: 1,
                label: '是'
            }]
        }
    },
    created() {
        this.getOrg(1)
        this.getAlliance()
    },
    computed: mapState({
        allianceList: state => state.Org.allianceList,
        page: state => state.Org.orgPage,
        tableData: state => state.Org.orgList
    }),
    methods: {
        ...mapActions(['saveOrg', 'saveAlliance']),
        search() {
            this.getOrg(this.page.number)
        },
        allianceFormatter(row) {
            if (row.alliance_id) {
                let name = ''
                this.allianceList.some(item => {
                    if (row.alliance_id == item.id) {
                        name = item.name
                    }
                })
                return name
            } else {
                return '暂无'
            }             
        },
        stateFormatter(row) {
            return row.state === 1 ? '启用' : '禁用'
        },
        isAllianceFormatter(row) {
            return row.isAlliancer === 1 ? '是' : '不是'
        },
        getOrg(number) {
            const params = {
                page: number,
                pageSize: 10
            }
            if (getStore('roleType') === 'allianceManager') {
                //机构智库管理
                params.orgId = getStore('roleTypeId')
            }
            if (this.select_word !== '') {
                params.searchKey = this.select_word
            }
            orgListApi({ ...params })
                .then(res => {
                    if (res.code === '000000') {
                        this.saveOrg({
                            ...res,
                            number
                        })
                    } else {
                        this.$message.error('网络错误')
                    }
                }, () => this.$message.error('网络错误'))
        },
        getAlliance() {
            listApi({ page: 1, pageSize: 100000 }).then(res => {
                if (res.code === '000000') {
                    this.saveAlliance(res)
                } else {
                    this.$message.error(res.message)
                }
            }, () => this.$message.error('服务器异常'))
        },
        handleCurrentChange(val) {
            this.getOrg(val)
        },
        delOrg(row) {
            this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteApi({ ids: row.id })
                    .then(res => {
                        console.log('删除机构', res)
                        if (res.code === '000000') {
                            this.getOrg(this.page.number)
                            this.$message.info('删除成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        add() {
            if (this.addForm.id) {
               updateApi({ ...this.addForm })
                    .then(res => {
                        if (res.code === '000000') {
                            this.dialogFormVisible = false
                            this.$message.info('修改成功')
                            this.getOrg(this.page.number)
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            } else {
                addApi({ ...this.addForm })
                    .then(res => {
                        console.log('机构', res)
                        if (res.code === '000000') {
                            this.dialogFormVisible = false
                            this.$message.info('添加成功')
                            this.getOrg(this.page.number)
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }
        },
        addEx() {
            this.title = '添加机构'
            this.dialogFormVisible = true
            this.addForm = {}
        },
        editOrg(row) {
            this.dialogFormVisible = true
            this.title = '修改机构'
            this.addForm = filterObj(row)
        },
        switchChange(row) {
            disableApi({ 
                ids: row.id, 
                state: row.state ? '0' : '1' 
            }).then(res => {
                if (res.code === '000000') {
                    this.$message.success('修改成功')
                    this.getOrg(this.page.number)
                } else {
                    this.$message.error(res.message)
                }
            }, () => {
                this.$message.error('网络错误')
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>
